package com.zzyl.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ResourceMapper;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.NoProcessing;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeItemVo;
import com.zzyl.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;


    @Override
    public List<ResourceVo> resourceList(ResourceDto resourceDto) {
        return  resourceMapper.resourceList(resourceDto);
    }
    @Override
    public TreeVo tree(ResourceDto resourceDto) {
        List<ResourceVo> resourceVoList = resourceMapper.resourceList(resourceDto);
        TreeVo treeVo = TreeVo.builder().items(new ArrayList<>()).build();

        //构建根
        TreeItemVo rootItemVo = TreeItemVo.builder().id(SuperConstant.ROOT_PARENT_ID).label("智慧养老院").children(new ArrayList<>()).build();
        treeVo.getItems().add(rootItemVo);
        recursionTreeItem(rootItemVo,resourceVoList);

        return treeVo;
    }

    @Override
    public void add(ResourceDto resourceDto) {
        //将Dto转实体
        Resource resource = BeanUtil.toBean(resourceDto, Resource.class);
        //查找父资源
        Resource parentResource = resourceMapper.selectByResourceNo(resourceDto.getParentResourceNo());
        resource.setDataState(parentResource.getDataState());
        boolean isIgnore = false;
        if(resourceDto.getResourceType().equals(SuperConstant.BUTTON)){
            isIgnore = true; //如果是按钮忽略层级
        }

        //创建资源编号
        String resourceNo = createResourceNo(resource.getParentResourceNo(), false);
        resource.setResourceNo(resourceNo);
        resourceMapper.insert(resource);

    }

//    @Override
//    public void update(ResourceDto resourceDto) {
//        //将Dto转实体
//        Resource resource = BeanUtil.toBean(resourceDto, Resource.class);
//
//        resourceMapper.update(resource);
//
//    }

    /**
     * 创建资源的编号
     * @param parentResourceNo
     * @return
     */
    private String createResourceNo(String parentResourceNo,boolean isIgnore) {

        //判断资源编号是否大于三级
        //100 001 000 000 000
        //100 001 001 000 000
        //100 001 001 001 000
        //100 001 001 001 001 001
        if(isIgnore && NoProcessing.processString(parentResourceNo).length() / 3 >= 5){
            throw new BaseException(BasicEnum.RESOURCE_DEPTH_UPPER_LIMIT);
        }

        //根据父资源编号查询子资源
        ResourceDto dto = ResourceDto.builder().parentResourceNo(parentResourceNo).build();
        List<ResourceVo> resources = resourceMapper.resourceList(dto);
        if(EmptyUtil.isNullOrEmpty(resources)){
            //无下属节点，创建新的节点编号  100 001 001 001 000--->100 001 001 001 001
            return NoProcessing.createNo(parentResourceNo,false);
        }else {
            //有下属节点，在已有的节点上追加
            //先获取已有节点的最大值--100001003000000
            Long maxNo = resources.stream().map(resource -> {
                return Long.valueOf(resource.getResourceNo());
            }).max(Comparator.comparing(i -> i)).get();

            return NoProcessing.createNo(String.valueOf(maxNo),true);
        }
    }

    private void recursionTreeItem(TreeItemVo rootItemVo, List<ResourceVo> resourceVoList) {
        for (ResourceVo vo : resourceVoList){
            if (rootItemVo.getId().equals(vo.getParentResourceNo()) && vo.getResourceType().equals(SuperConstant.MENU)){
                TreeItemVo itemVo = TreeItemVo.builder().id(vo.getResourceNo()).label(vo.getResourceName()).children(new ArrayList<>()).build();
                rootItemVo.getChildren().add(itemVo);
                recursionTreeItem(itemVo,resourceVoList);
            }
        }
    }

}
